package com.itheima.Controller;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class User {
    Scanner sc = new Scanner(System.in);

    public void modify() throws IOException {
        System.out.println("修改密码");
        ArrayList<Nema> list = getNemas();
        String userName = null;
        aaa:
        while (true) {
            System.out.println("请输入要修改用户名(输入exit返回上一级)");
            userName = sc.next();
            if("exit".equals(userName)){
                return;
            }
            for (Nema nema : list) {
                String userName1 = nema.getUserName();
                if (userName1.equals(userName)) {
                    break aaa;
                }
            }
            System.out.println("没有该用户,请重新输入");
        }
        System.out.println("请输入原密码");
        String psssword = sc.next();
        for (Nema nema : list) {
            String userName1 = nema.getUserName();
            String password1 = nema.getPassword();
            if (userName1.equals(userName) && password1.equals(psssword)) {
                while (true) {
                    System.out.println("请输入新密码");
                    String newpassword = sc.next();
                    System.out.println("请重复输入密码");
                    String newpassword1 = sc.next();

                    if (newpassword.equals(newpassword1)) {
                        if (!newpassword.contains(",")) {
                            System.out.println("密码修改成功");
                            nema.setPassword(newpassword);
                            break;
                        } else {
                            System.out.println("密码不能含有特殊符号逗号,请重新输入");
                        }

                    } else {
                        System.out.println("密码不一致");
                    }
                }
            }else {
                System.out.println("密码错误,请重新输入用户名和密码");
                modify();
                return;
            }
        }
        BufferedWriter bw = new BufferedWriter(new FileWriter("reptileDemozcy\\books\\name.txt"));
        for (Nema nema1 : list) {
            bw.write(nema1.getUserName() + "," + nema1.getPassword());
            bw.newLine();
        }

        bw.close();
    }

    //登录
    public boolean Login() throws IOException {
        boolean flag = false;
        while (true) {
            ArrayList<Nema> list = getNemas();
            System.out.println("请输入用户名(输入exit返回上一级)");
            String userName = sc.next();
            if("exit".equals(userName)){
                return false;
            }
            System.out.println("请输入密码");
            String psssword = sc.next();
            for (Nema nema : list) {
                String userName1 = nema.getUserName();
                String password1 = nema.getPassword();
                if (userName1.equals(userName) && password1.equals(psssword)) {
                    flag = true;
                }

            }
            if (flag) {
                System.out.println("登陆成功");


                break;
            } else {
                System.out.println("登录失败");
            }
        }
        return flag;

    }

    public ArrayList<Nema> getNemas() throws IOException {

        ArrayList<Nema> list = new ArrayList<>();
        File file = new File("reptileDemozcy\\books\\name.txt");
        if (!file.exists()){
            file.createNewFile();
        }
        BufferedReader br = new BufferedReader(new FileReader(file));
        String len;
        while ((len = br.readLine()) != null) {
            String[] split = len.split(",");
            Nema nema1 = new Nema(split[0], split[1]);
            list.add(nema1);
        }
        br.close();
        return list;
    }

    public void add() throws IOException {
        ArrayList<Nema> list = getNemas();
        System.out.println("请输入用户名(输入exit返回上一级)");
        String userName = sc.next();
        if ("exit".equals(userName)){
            return;
        }
        for (Nema nema : list) {
            String userName1 = nema.getUserName();
            while (true) {
                if (userName1 == null) {
                    break;
                }
                if (userName1.equals(userName)) {
                    System.out.println("用户名重复，请重新输入");
                    System.out.println("请输入用户名");
                    userName = sc.next();
                } else {
                    break;
                }
            }
        }
        String password;
        while (true) {
            System.out.println("请输入新密码");
            password = sc.next();
            System.out.println("请重复输入密码");
            String newpassword = sc.next();

            if (password.equals(newpassword)) {
                if (!password.contains(",")) {
                    break;
                } else {
                    System.out.println("密码不能含有特殊符号逗号,请重新输入");
                }
            } else {
                System.out.println("密码不一致");
            }

        }
        Nema nema = new Nema();
        nema.setUserName(userName);
        nema.setPassword(password);

        list.add(nema);

        BufferedWriter bw = new BufferedWriter(new FileWriter("reptileDemozcy/books/name.txt"));
        for (Nema nema1 : list) {
            bw.write(nema1.getUserName() + "," + nema1.getPassword());
            bw.newLine();
        }

        bw.close();
        System.out.println("注册成功");
    }
}
